import 'package:flick_video_player/flick_video_player.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:video_player/video_player.dart';

import 'landscape_player_controls.dart';

class VideoPlayerM extends StatefulWidget {
  final String url;
  final String icon;
  final String title;

  const VideoPlayerM(
      {super.key, required this.url, required this.icon, required this.title});

  @override
  State<VideoPlayerM> createState() => _MyVideoPlayerM();
}

class _MyVideoPlayerM extends State<VideoPlayerM> {
  late FlickManager flickManager;

  /*创建界面事件*/
  @override
  void initState() {
    super.initState();
    SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft]);
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []); // 隐藏状态栏
    flickManager = FlickManager(
      videoPlayerController: VideoPlayerController.network(widget.url),
    );
  }

  /*销毁界面事件*/
  @override
  void dispose() {
    flickManager.dispose();
    SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual,
        overlays: SystemUiOverlay.values);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        /*返回键事件*/
        SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
        Navigator.pop(context);
        return true;
      },
      child: Scaffold(
        body: FlickVideoPlayer(
          flickManager: flickManager,
          preferredDeviceOrientation: const [
            DeviceOrientation.landscapeRight,
            DeviceOrientation.landscapeLeft,
          ],
          systemUIOverlay: const [],
          flickVideoWithControls: const FlickVideoWithControls(
            controls: LandscapePlayerControls(),
          ),
          flickVideoWithControlsFullscreen: const FlickVideoWithControls(
            controls: FlickLandscapeControls(),
          ),
        ),
      ),
    );
  }
}
